/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.ChuyenTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class ChuyenTau_DAO {
    public static String Get_MaCT() throws SQLException{
        String MaCT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "CT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            MaCT = Rs.getString(1);
        }
        return MaCT;
    }
    
    public static ArrayList<ChuyenTau_DTO> GetALL()
    {
        ArrayList<ChuyenTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from ChuyenTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                ChuyenTau_DTO ChuyenTau = new ChuyenTau_DTO();
                ChuyenTau.setMaCT(rs.getString("MaCT"));
                ChuyenTau.setTenCT(rs.getString("TenCT"));
                ChuyenTau.setMaDT(rs.getString("MaDT"));
                ChuyenTau.setMaTT(rs.getString("MaTT"));
                ChuyenTau.setNgayXP(rs.getDate("NgayXP"));
                ChuyenTau.setMaGaDi(rs.getString("MaGaDi"));
                ChuyenTau.setMaGaDen(rs.getString("MaGaDen"));
                ds.add(ChuyenTau);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(ChuyenTau_DTO ChuyenTau)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayXP = fmt.format(ChuyenTau.getNgayXP());
            String sql = "Insert ChuyenTau Values('"+ChuyenTau.getMaCT()+"','"+ChuyenTau.getTenCT()+"','"+ChuyenTau.getMaDT()+"','"+ChuyenTau.getMaTT()+"','"+NgayXP+"','"+ChuyenTau.getMaGaDi()+"','"+ChuyenTau.getMaGaDen()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(ChuyenTau_DTO ChuyenTau)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayXP = fmt.format(ChuyenTau.getNgayXP());
            String sql = "Update ChuyenTau Set TenCT = '"+ ChuyenTau.getTenCT() +"', MaDT = '"+ChuyenTau.getMaDT()+"', MaTT = '"+ChuyenTau.getMaTT()+"', NgayXP = '"+NgayXP+ "', MaGaDi = '"+ChuyenTau.getMaGaDi()+"', MaGaDen ='"+ChuyenTau.getMaGaDen()+"' where MaCT = '"+ ChuyenTau.getMaCT()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maCT)
    {
        boolean flag = false;
        try {
            String sql = "Delete from ChuyenTau where MaCT = '"+maCT+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
